package com.baiyun.bs.controller.admin;

import com.baiyun.bs.base.RestResponse;
import com.baiyun.bs.domain.Course;
import com.baiyun.bs.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 视频
 */

@RestController("AdminCoursesController")
@RequestMapping(value = "/api/admin/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public RestResponse<List<Course>> getAllCourses(@RequestBody Course course) {
        try {
            List<Course> courses = courseService.getAllCourses(course);
            return RestResponse.ok(courses);
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();
            // 捕获异常并返回包含错误信息的响应
            return RestResponse.fail(500, "系统内部错误");
        }

    }

    @RequestMapping(value = "/get/{courseId}", method = RequestMethod.POST)
    public RestResponse<Course> getCourseById(@PathVariable Long courseId) {
        Course course = courseService.getCourseById(courseId);
        return RestResponse.ok(course);
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse<Void> addCourse(@RequestBody Course course) {
        if (course.getCourseId() != null) {
            courseService.updateCourse(course);
        }else {
            courseService.addCourse(course);
        }
        return RestResponse.ok();
    }
    @RequestMapping(value = "/update/{courseId}", method = RequestMethod.POST)
    public  RestResponse<Void> updateCourse(@PathVariable Long courseId, @RequestBody Course course) {
        course.setCourseId(courseId);
        courseService.updateCourse(course);
        return RestResponse.ok();
    }
    @RequestMapping(value = "/delete/{courseId}", method = RequestMethod.POST)
    public RestResponse<Void> deleteCourse(@PathVariable Long courseId) {
        courseService.deleteCourse(courseId);
        return RestResponse.ok();
    }
}
